/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package putime.model;

import java.sql.SQLException;
import java.sql.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import putime.db.DBModel;
import putime.newspaperagent.line.Line;
import putime.newspaperagent.supplier.Supplier;

/**
 *
 * @author Khai
 */
public class ModelLine extends DBModel {

    Vector<Line> vLine;

    public Vector<Line> initComboxLine() {
        vLine = new Vector<>();
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_lines");
            rs = prst.executeQuery();
            while (rs.next()) {
                Line line = new Line(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDate(5));
                vLine.add(line);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        }
        closeConnect();
        return vLine;
    }

    public Line ComboChange(int chon) {
        openConnect();
        Line s = null;
        try {
            prst = conn.prepareStatement("select * from tbl_lines where LineID = ?");
            prst.setInt(1, chon);
            rs = prst.executeQuery();
            rs.next();
            s = new Line(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDate(5));
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        }
        closeConnect();
        return s;

    }

    public boolean deleteLine(int chon) {
        openConnect();
        try {
            prst = conn.prepareStatement("delete from tbl_lines where LineID = ?");
            prst.setInt(1, chon);
            prst.executeUpdate();
            closeConnect();
            return true;
        } catch (SQLException ex) {
            closeConnect();
            return false;
        }
    }

    public boolean editLine(int chon, String name, String address, String telephone, Date hireDate) {
        openConnect();
        try {
            prst = conn.prepareStatement("update tbl_lines set Name = ?, Address = ?, Tel = ?, Hiredate = ? where LineID = ?");
            prst.setString(1, name);
            prst.setString(2, address);
            prst.setString(3, telephone);
            prst.setDate(4, hireDate);
            prst.setInt(5, chon);
            prst.executeUpdate();
            closeConnect();
            return true;
        } catch (SQLException ex) {
            closeConnect();
            return false;
        }
    }

    public boolean addSupplier(String name, String address, String telephone, Date hireDate) {
        openConnect();
        try {
            prst = conn.prepareStatement("insert tbl_lines(Name,Address,Tel,Hiredate) values (?,?,?,?)");
            prst.setString(1, name);
            prst.setString(2, address);
            prst.setString(3, telephone);
            prst.setDate(4, hireDate);
            prst.executeUpdate();
            closeConnect();
            return true;
        } catch (SQLException ex) {
            closeConnect();
            return false;
        }                     
    }
    
    
    public Vector<Line> findID(int chon) {
         Vector<Line> vLine = new Vector<>();
        Line line;
        openConnect();       
        try {
            prst = conn.prepareStatement("select * from tbl_lines where LineID = ?");
            prst.setInt(1, chon);
            rs = prst.executeQuery();
            while (rs.next()) {
                 line = new Line(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getDate(5));
                vLine.add(line);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }
         return vLine;
    }

    public Vector<Line> findName(String chon) {
        Vector<Line> vLine = new Vector<>();
        Line line;
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_lines where Name LIKE '%" + chon + "%'");
            rs = prst.executeQuery();
            while (rs.next()) {
                line = new Line(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getDate(5));
                vLine.add(line);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }

        return vLine;
    }
    
    public Vector<Line> findAddress(String chon) {
        Vector<Line> vLine = new Vector<>();
        Line line;
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_lines where Address LIKE '%" + chon + "%'");
            rs = prst.executeQuery();
            while (rs.next()) {
                line = new Line(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getDate(5));
                vLine.add(line);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }

        return vLine;
    }
     
      public Vector<Line> findTel(String chon) {
        Vector<Line> vLine = new Vector<>();
        Line line;
        openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_lines where Tel LIKE '%" + chon + "%'");
            rs = prst.executeQuery();
            while (rs.next()) {
                line = new Line(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getDate(5));
                vLine.add(line);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelSupplier.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnect();
        }

        return vLine;
    }
      public boolean checkExist(String name){
          openConnect();
        try {
            prst = conn.prepareStatement("select * from tbl_lines where Name = ?");
            prst.setString(1, name);
            rs = prst.executeQuery();
            while(rs.next()){
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(ModelLine.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            closeConnect();
        }
        return false;
          
      }
}
